<?php
/**
 * the base item controller manages the basic functionality that a content item needs to save
 *
 */
class DigCms_Library_Item_Controller extends Zend_Controller_Action
{
    protected $_modelClass = 'DigCms_Model_Item';
    protected $_formClass = 'DigCms_Form_Item';
    protected $_model;
    protected $_form;
    const CURRENT_ITEM = 'DigCms_Item';

    /**
     * sets up the base options for the item controllers
     */
    public function init()
    {
        // attempt to map the model and form classes automatically
        $itemResources = DigCms_Service_Item::mapControllerToResources(get_class($this));
        if($itemResources['model']) {
            $this->_modelClass = $itemResources['model'];
        }
        if($itemResources['form']) {
            $this->_formClass = $itemResources['form'];
        }

        Dig_Service_Firebug::profile('initializing base item controller');
        // validate
        if(empty($this->_modelClass)) {
            throw new Dig_Exception('The model has not been set in ' . get_class($this));
        }
        if(empty($this->_formClass)) {
            throw new Dig_Exception('The form has not been set in ' . get_class($this));
        }
        // on a public request the navigation routing plugin creates an instance of the
        // proper item and passes it as a request parameter
        $this->_model = $this->getRequest()->getParam(self::CURRENT_ITEM);
        if($this->_model == null) {
            $this->_model = new $this->_modelClass($this->getRequest()->getParam('id'));
        }
        Dig_Service_Firebug::profile('instantiated model');
        $type = DigCms_Service_Item::getItemTypeDetails($this->_model->namespace);
        Dig_Service_Firebug::profile('loaded the item details');
        if(!$type) {
            $type = DigCms_Service_Item::getDefaultType();
        }

        if(! isset($this->_form)) {
            if($this->getRequest()->getActionName() == 'form' || $this->getRequest()->getActionName() == 'save') {
                $this->_form = new $this->_formClass();
                $this->_form->setAction('/' . $type['module'] . '/' . $type['controller'] . '/save');
                Dig_Service_Firebug::profile('created form instance for model');
                $this->_form->populate($this->_model);
                Dig_Service_Firebug::profile('loaded form with model data');
            }
        }
        $this->view->item = $this->_model;
        Dig_Service_Firebug::profile('finished initializing base item controller');
    }

    /**
     * the render action must be implemented in your controller
     */
    public function renderAction()
    {
        throw new Dig_Exception('Required render action not implemented in ' . get_class($this));
    }

    /**
     * renders the form
     */
    public function formAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        echo $this->_form;
    }

    /**
     *
     * saves the item
     */
    public function saveAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        if($this->_form->validatePost()) { 
            $this->view->digUiMessage()->addMessage( $this->_model->name . ' was successfully updated.', DigUi_View_Helper_DigUiMessage::INFO );
            $this->_model->save( $this->_form->getValues());
        }
        return $this->_forward('form');
    }
}
?>